#!/bin/bash
# script name: K8s 环境部署脚本
# author: subushi
# update date: 2023.07.24

# 没有 tips 提示则所有节点都需执行

# 设置主机名
hostnamectl set-hostname k8s-master
#hostnamectl set-hostname k8s-node1
#hostnamectl set-hostname k8s-node2


# 关闭冲突服务
systemctl stop firewalld
systemctl disable firewalld
systemctl stop postfix
systemctl disable postfix
systemctl stop NetworkManager
systemctl disable NetworkManager
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab


# 配置转发相关参数
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.ipv4.tcp_tw_recycle = 0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
EOF

sysctl --system


# 安装 docker
yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum install -y   docker-ce-24.0.2   docker-ce-cli-24.0.2   containerd.io-1.4.6


# 导入 docker 配置
mkdir -p /etc/docker

cat <<EOF | tee /etc/docker/daemon.json
{
  "registry-mirrors": ["https://bp857gis.mirror.aliyuncs.com"],
  "log-driver":"json-file",
  "log-opts": {"max-size":"500m", "max-file":"3"},
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF


# 启动 docker
systemctl daemon-reload
systemctl enable --now docker


# 安装 cri-dockerd---(需挂代理)
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.1/cri-dockerd-0.3.1-3.el7.x86_64.rpm
rpm -ivh cri-dockerd-0.3.1-3.el7.x86_64.rpm
rpm -ql cri-dockerd
sed -i 's/^ExecStart.*/ExecStart=\/usr\/bin\/cri-dockerd --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com\/google_containers\/pause:3.9/g' /usr/lib/systemd/system/cri-docker.service
systemctl daemon-reload
systemctl enable --now cri-docker


# 加载 ipvs 相关内核模块
cat <<EOF | tee /home/mod.sh
#!/bin/bash
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
modprobe br_netfilter
EOF
chmod +x /home/mod.sh
bash /home/mod.sh


# 设置开机自启，查看是否成功加载
echo 'bash /home/mod.sh' >> /etc/rc.d/rc.local
chmo +x /etc/rc.d/rc.local
lsmod | grep ip_vs


# 安装 kubernetes
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum install -y   kubectl-1.26.2   kubeadm-1.26.2   kubelet-1.26.2   ipvsadm   ipset


# 启动 kubernetes
systemctl enable --now kubelet


# 拉取 K8s 组件镜像
k8sversion="v1.26.2"
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:${k8sversion}
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:${k8sversion}
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:${k8sversion}
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:${k8sversion}
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.9.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.6-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9


# 所有主机添加 master 域名映射
echo '192.168.10.1 k8s-master' >> /etc/hosts


# 测试 ping
ping -c 5 k8s-master


#------以下操作手动执行，根据个人IP和k8s版本号进行修改，网络插件 calico/flannel 2选1

# 初始化主节点---(tips:master)
#kubeadm init --apiserver-advertise-address=192.168.10.1 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.26.2 --service-cidr=10.168.0.0/12 --pod-network-cidr=10.244.0.0/16 --cri-socket unix:///var/run/cri-dockerd.sock  --ignore-preflight-errors=all


# 拉取 calico 网络插件
#calversion="v3.25.0"
#docker pull calico/cni:${calversion}
#docker pull calico/node:${calversion}
#docker pull calico/kube-controllers:${calversion}
#docker pull calico/pod2daemon-flexvol:${calversion}


# 拉取 flannel 网络插件---(根据kube-flannel.yml的 152和179行 判定需拉取的镜像版本)---(master和node都需拉取镜像，拉取后在master执行即可)---(需挂代理)
#wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
#docker pull docker.io/flannel/flannel:v0.22.0
#docker pull docker.io/flannel/flannel-cni-plugin:v1.1.2

#kubectl apply -f kube-flannel.yml


# 于 master 查看节点状态---(处于Ready/Running状态即可)
#kubectl get nodes
#kubectl get pods -n kube-system


# 加入集群，后面一定要加上 "--cri-socket unix:///var/run/cri-dockerd.sock" 这一参数，否则可能会失败---(tips:node)
#kubeadm join 192.168.10.1:6443 --token ihtles.bk1vscwto5e3bl5l --discovery-token-ca-cert-hash sha256:03002a4dc610a3d3766b8a896a9f56003120b31b8f89731f4a15664f2bfc888c  --cri-socket unix:///var/run/cri-dockerd.sock


# kubectl 命令行自动补齐---(tips:master)
#yum install bash-completion  &&  echo 'source /usr/share/bash-completion/bash_completion
#source <(kubectl completion bash)' >> /root/.bashrc  &&  source /root/.bashrc
